class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int sum = 0, n = nums.size();;
        for(auto& e : nums)
        {
            sum += e;
        }
        int a = (sum + target) / 2;
        if(a < 0 || (sum + target) % 2) // 小于0或者除不尽
            return 0;
 
        vector<vector<int>> dp(n + 1, vector<int>(a + 1, 0));
        dp[0][0] = 1;
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 0; j <= a; ++j) // 第1列用到dp[0][0]初始化
            {
                if(j >= nums[i - 1]) // 注意原数组下标映射
                    dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i - 1]];
                else
                    dp[i][j] = dp[i - 1][j];
            }
        }
        return dp[n][a];
    }
};